package com.irdstudio.efp.flow.service.impl;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SEQ;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.flow.service.dao.BizPluginParamDao;
import com.irdstudio.efp.flow.service.dao.BizPluginServiceDao;
import com.irdstudio.efp.flow.service.domain.BizPluginService;
import com.irdstudio.efp.flow.service.facade.BizPluginServiceService;
import com.irdstudio.efp.flow.service.vo.BizPluginServiceVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.SequenceConstance;
/**
 * <p>ServiceImpl: 业务服务插件信息				<p>
 * @author ligm
 * @date 2018-05-05
 */
@Service("bizPluginServiceService")
public class BizPluginServiceServiceImpl extends AbstractFrameworkService implements BizPluginServiceService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizPluginServiceServiceImpl.class);

	@Autowired
	private BizPluginServiceDao bizPluginServiceDao;
	
	@Autowired
	private BizPluginParamDao bizPluginParamDao;

	@Autowired
	private PatternedLimitableSeqService sequenceService;
	
	public static final PatternDescriptor PATTERN = DefaultPatternDescriptor.of("PS" + SEQ, 6);
	
	/**
	 * 新增操作
	 */
	public int insertBizPluginService(BizPluginServiceVO inBizPluginServiceVo) {
		logger.debug("当前新增数据为:"+ inBizPluginServiceVo.toString());
		int num = 0;
		try {
			BizPluginService bizPluginService = new BizPluginService();
			inBizPluginServiceVo.setBizPluginId(sequenceService.getPatternedSequence(SequenceConstance.BIZ_PlUGIN_SRV_SEQ_ID, PATTERN));
			beanCopy(inBizPluginServiceVo, bizPluginService);
			num = bizPluginServiceDao.insertBizPluginService(bizPluginService);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}
	/**
	 * 新增操作
	 */
	public BizPluginServiceVO insertBizPluginServiceBack(BizPluginServiceVO inBizPluginServiceVo) {
		logger.debug("当前新增数据为:"+ inBizPluginServiceVo.toString());
		int num = 0;
		try {
			BizPluginService bizPluginService = new BizPluginService();
			inBizPluginServiceVo.setBizPluginId(sequenceService.getPatternedSequence(SequenceConstance.BIZ_PlUGIN_SRV_SEQ_ID, PATTERN));
			beanCopy(inBizPluginServiceVo, bizPluginService);
			num = bizPluginServiceDao.insertBizPluginService(bizPluginService);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return inBizPluginServiceVo;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizPluginServiceVO inBizPluginServiceVo) {
		logger.debug("当前删除数据条件为:"+ inBizPluginServiceVo);
		int num = 0;
		try {
			BizPluginService bizPluginService = new BizPluginService();
			beanCopy(inBizPluginServiceVo, bizPluginService);
			num = bizPluginServiceDao.deleteByPk(bizPluginService);
			bizPluginParamDao.deleteByPluginId(bizPluginService.getBizPluginId());
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizPluginServiceVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizPluginServiceVO inBizPluginServiceVo) {
		logger.debug("当前修改数据为:"+ inBizPluginServiceVo.toString());
		int num = 0;
		try {
			BizPluginService bizPluginService = new BizPluginService();
			beanCopy(inBizPluginServiceVo, bizPluginService);
			num = bizPluginServiceDao.updateByPk(bizPluginService);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizPluginServiceVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizPluginServiceVO queryByPk(BizPluginServiceVO inBizPluginServiceVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizPluginServiceVo);
		try {
			BizPluginService queryBizPluginService = new BizPluginService();
			beanCopy(inBizPluginServiceVo, queryBizPluginService);
			BizPluginService queryRslBizPluginService = bizPluginServiceDao.queryByPk(queryBizPluginService);
			if (Objects.nonNull(queryRslBizPluginService)) {
				BizPluginServiceVO outBizPluginServiceVo = beanCopy(queryRslBizPluginService, new BizPluginServiceVO());
				logger.debug("当前查询结果为:"+ outBizPluginServiceVo.toString());
				return outBizPluginServiceVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizPluginServiceVO> queryAllOwner(BizPluginServiceVO bizPluginServiceVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizPluginServiceVO> list = null;
		try {
			List<BizPluginService> bizPluginServices = bizPluginServiceDao.queryAllOwnerByPage(bizPluginServiceVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizPluginServices.size());
			pageSet(bizPluginServices, bizPluginServiceVo);
			list = (List<BizPluginServiceVO>) beansCopy(bizPluginServices, BizPluginServiceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizPluginServiceVO> queryAllCurrOrg(BizPluginServiceVO bizPluginServiceVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizPluginService> bizPluginServices = bizPluginServiceDao.queryAllCurrOrgByPage(bizPluginServiceVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizPluginServices.size());
		List<BizPluginServiceVO> list = null;
		try {
			pageSet(bizPluginServices, bizPluginServiceVo);
			list = (List<BizPluginServiceVO>) beansCopy(bizPluginServices, BizPluginServiceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizPluginServiceVO> queryAllCurrDownOrg(BizPluginServiceVO bizPluginServiceVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizPluginService> bizPluginServices = bizPluginServiceDao.queryAllCurrDownOrgByPage(bizPluginServiceVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizPluginServices.size());
		List<BizPluginServiceVO> list = null;
		try {
			pageSet(bizPluginServices, bizPluginServiceVo);
			list = (List<BizPluginServiceVO>) beansCopy(bizPluginServices, BizPluginServiceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
